<template>
  <div class='aui-card--fill scroll-box card-box'>
    <div class='pane-shell'>
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               label-width="50px">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="停车场对接平台设置"
                            name="1">
            <el-select v-model="dataForm.platformId"
                       clearable
                       style="width:500px"
                       placeholder="平台名称">
              <el-option v-for="item in platformList"
                         :key="item.id"
                         :label="item.platformName + '-' + getmanufacturersToString(item.manufacturers)"
                         :value="item.id">
              </el-option>
            </el-select>
            <el-button style="margin-left:15px"
                       v-if="$hasPermission('tb:platformConfiguration')"
                       @click="save(0, dataForm.platformId)">{{ $t("save") }}</el-button>
          </el-collapse-item>
          <el-collapse-item title="同步用户与组织架构对接平台设置"
                            name="2">
            <el-select v-model="dataForm.userplatformId"
                       clearable
                       style="width:500px"
                       placeholder="平台名称">
              <el-option v-for="item in platformList"
                         :key="item.id"
                         :label="item.platformName + '-' + getmanufacturersToString(item.manufacturers)"
                         :value="item.id">
              </el-option>
            </el-select>
            <el-button style="margin-left:15px"
                       v-if="$hasPermission('tb:platformConfiguration')"
                       @click="save(1, dataForm.userplatformId)">{{ $t("save") }}</el-button>
          </el-collapse-item>
        </el-collapse>
      </el-form>
    </div>
  </div>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  name: '',
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addOrUpdateURL: '', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        updateStatusURL: '',
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      dataForm: {
        platformId: '',
        userplatformId: ''
      },
      manufacturersList: [{ id: 0, name: '北森' }, // 第三方平台列表
        { id: 1, name: '泛微' },
        { id: 2, name: '企业微信' },
        { id: 3, name: '大华' },
        { id: 4, name: '海康' },
        { id: 5, name: '阿里云' },
        { id: 6, name: 'IPC广播网络平台' }],
      activeNames: ['1', '2'],
      platformList: []
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.getdata()
      this.getplatformList()
    })
  },
  methods: {
    getplatformList () {
      this.$http.get('tb/thirdPlatform/queryList').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (res.data) {
          this.platformList = res.data
          // manufacturers===10是无效数据
          this.platformList.forEach((item, index) => {
            if (item.manufacturers === 10) {
              this.platformList.splice(index, 1)
            }
          })
        }
      })
    },
    async getdata () {
      this.$http.get('tb/platformConfiguration/list?type=0').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (res.data) {
          this.dataForm.platformId = res.data[0].platformId
        }
      })
      this.$http.get('tb/platformConfiguration/list?type=1').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        if (res.data) {
          this.dataForm.userplatformId = res.data[0].platformId
        }
      })
    },
    getmanufacturersToString (_id) {
      return this.manufacturersList.find(item => item.id === _id).name
    },
    // 表单提交
    save: debounce(
      function (_type, _id) {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            let obj = {
              type: _type,
              platformId: _id
            }
            this.$http.post(`tb/platformConfiguration`, obj).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message.success('保存成功')
              this.getdata()
            })
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>
<style lang="scss" scoped>
</style>
